package org.scut.smartgeometry.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import org.scut.smartgeometry.R;
import org.scut.smartgeometry.act.adapter.GridImageAdapter;
import org.scut.smartgeometry.utils.FileUtils;

/**
 * Created by Administrator on 13-6-28.
 */
public class OpenDialog extends Dialog {

    private OpenCallBack openCallBack;
    private List<Bitmap> bitmaps;

    public OpenDialog(Context context, OpenCallBack openCallBack) {
        super(context);
        this.openCallBack = openCallBack;
        getBitmaps();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("Select a File");

        View linearLayout = getLayoutInflater().inflate(R.layout.open_dia, null);

        setContentView(linearLayout);

        GridView gridView = (GridView) linearLayout.findViewById(R.id.gridView_open);

        gridView.setAdapter(new GridImageAdapter(getContext(), translateBitmapsToDrawabls(), gridView));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Log.i("hb", "i == " + i);
                openCallBack.openForResult(bitmaps.get(i));

                bitmaps = null;
                dismiss();
            }
        });
    }

    public interface OpenCallBack{
        void openForResult(Bitmap bitmap);
    }

    private Drawable[] translateBitmapsToDrawabls(){
        Drawable[] drawables = new Drawable[bitmaps.size()];

        for (int i = 0; i<bitmaps.size(); i++){
            drawables[i] = new BitmapDrawable(getContext().getResources(), bitmaps.get(i));
        }

        return drawables;
    }

    private void getBitmaps(){
        bitmaps = FileUtils.getBitmaps();
    }

}
